home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / tcl / tclGet.c < prev    next >
C/C++ Source or Header  |  1993-02-13  |  5KB  |  181 lines

  1. /* 
  2.  * tclGet.c --
  3.  *
  4.  *    This file contains procedures to convert strings into
  5.  *    other forms, like integers or floating-point numbers or
  6.  *    booleans, doing syntax checking along the way.
  7.  *
  8.  * Copyright 1990-1991 Regents of the University of California
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software and its documentation for any purpose and without
  11.  * fee is hereby granted, provided that the above copyright
  12.  * notice appear in all copies.  The University of California
  13.  * makes no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without
  15.  * express or implied warranty.
  16.  */
  17.  
  18. #include "tclInt.h"
  19.  
  20. /*
  21.  *----------------------------------------------------------------------
  22.  *
  23.  * Tcl_GetInt --
  24.  *
  25.  *    Given a string, produce the corresponding integer value.
  26.  *
  27.  * Results:
  28.  *    The return value is normally TCL_OK;  in this case *intPtr
  29.  *    will be set to the integer value equivalent to string.  If
  30.  *    string is improperly formed then TCL_ERROR is returned and
  31.  *    an error message will be left in interp->result.
  32.  *
  33.  * Side effects:
  34.  *    None.
  35.  *
  36.  *----------------------------------------------------------------------
  37.  */
  38.  
  39. int
  40. Tcl_GetInt(interp, string, intPtr)
  41.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  42.     char *string;        /* String containing a (possibly signed)
  43.                  * integer in a form acceptable to strtol. */
  44.     int *intPtr;        /* Place to store converted result. */
  45. {
  46.     char *end;
  47.     int i;
  48.  
  49.     i = strtol(string, &end, 0);
  50.     while ((*end != '\0') && isspace(*end)) {
  51.     end++;
  52.     }
  53.     if ((end == string) || (*end != 0)) {
  54.     Tcl_AppendResult(interp, "expected integer but got \"", string,
  55.         "\"", (char *) NULL);
  56.     return TCL_ERROR;
  57.     }
  58.     *intPtr = i;
  59.     return TCL_OK;
  60. }
  61.  
  62. /*
  63.  *----------------------------------------------------------------------
  64.  *
  65.  * Tcl_GetDouble --
  66.  *
  67.  *    Given a string, produce the corresponding double-precision
  68.  *    floating-point value.
  69.  *
  70.  * Results:
  71.  *    The return value is normally TCL_OK;  in this case *doublePtr
  72.  *    will be set to the double-precision value equivalent to string.
  73.  *    If string is improperly formed then TCL_ERROR is returned and
  74.  *    an error message will be left in interp->result.
  75.  *
  76.  * Side effects:
  77.  *    None.
  78.  *
  79.  *----------------------------------------------------------------------
  80.  */
  81.  
  82. int
  83. Tcl_GetDouble(interp, string, doublePtr)
  84.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  85.     char *string;        /* String containing a floating-point number
  86.                  * in a form acceptable to strtod. */
  87.     double *doublePtr;        /* Place to store converted result. */
  88. {
  89.     char *end;
  90.     double d;
  91.  
  92.     d = strtod(string, &end);
  93.     while ((*end != '\0') && isspace(*end)) {
  94.     end++;
  95.     }
  96.     if ((end == string) || (*end != 0)) {
  97.     Tcl_AppendResult(interp, "expected floating-point number but got \"",
  98.         string, "\"", (char *) NULL);
  99.     return TCL_ERROR;
  100.     }
  101.     *doublePtr = d;
  102.     return TCL_OK;
  103. }
  104.  
  105. /*
  106.  *----------------------------------------------------------------------
  107.  *
  108.  * Tcl_GetBoolean --
  109.  *
  110.  *    Given a string, return a 0/1 boolean value corresponding
  111.  *    to the string.
  112.  *
  113.  * Results:
  114.  *    The return value is normally TCL_OK;  in this case *boolPtr
  115.  *    will be set to the 0/1 value equivalent to string.  If
  116.  *    string is improperly formed then TCL_ERROR is returned and
  117.  *    an error message will be left in interp->result.
  118.  *
  119.  * Side effects:
  120.  *    None.
  121.  *
  122.  *----------------------------------------------------------------------
  123.  */
  124.  
  125. int
  126. Tcl_GetBoolean(interp, string, boolPtr)
  127.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  128.     char *string;        /* String containing a boolean number
  129.                  * specified either as 1/0 or true/false or
  130.                  * yes/no. */
  131.     int *boolPtr;        /* Place to store converted result, which
  132.                  * will be 0 or 1. */
  133. {
  134.     char c;
  135.     char lowerCase[10];
  136.     int i, length;
  137.  
  138.     /*
  139.      * Convert the input string to all lower-case.
  140.      */
  141.  
  142.     for (i = 0; i < 9; i++) {
  143.     c = string[i];
  144.     if (c == 0) {
  145.         break;
  146.     }
  147.     if ((c >= 'A') && (c <= 'Z')) {
  148.         c += 'a' - 'A';
  149.     }
  150.     lowerCase[i] = c;
  151.     }
  152.     lowerCase[i] = 0;
  153.  
  154.     length = strlen(lowerCase);
  155.     c = lowerCase[0];
  156.     if ((c == '0') && (lowerCase[1] == '\0')) {
  157.     *boolPtr = 0;
  158.     } else if ((c == '1') && (lowerCase[1] == '\0')) {
  159.     *boolPtr = 1;
  160.     } else if ((c == 'y') && (strncmp(lowerCase, "yes", length) == 0)) {
  161.     *boolPtr = 1;
  162.     } else if ((c == 'n') && (strncmp(lowerCase, "no", length) == 0)) {
  163.     *boolPtr = 0;
  164.     } else if ((c == 't') && (strncmp(lowerCase, "true", length) == 0)) {
  165.     *boolPtr = 1;
  166.     } else if ((c == 'f') && (strncmp(lowerCase, "false", length) == 0)) {
  167.     *boolPtr = 0;
  168.     } else if ((c == 'o') && (length >= 2)) {
  169.     if (strncmp(lowerCase, "on", length) == 0) {
  170.         *boolPtr = 1;
  171.     } else if (strncmp(lowerCase, "off", length) == 0) {
  172.         *boolPtr = 0;
  173.     }
  174.     } else {
  175.     Tcl_AppendResult(interp, "expected boolean value but got \"",
  176.         string, "\"", (char *) NULL);
  177.     return TCL_ERROR;
  178.     }
  179.     return TCL_OK;
  180. }
  181.